package com.gongpingjialoan.activity.tool;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.AppRecomendAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.AppRecomendBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2016/4/18.
 */
public class AppRecommendActivity extends BaseActivity {

    private NetDataJson mNetDataJson;

    private LoadingDialog loadingDialog;

    private ListView mListView;

    private AppRecomendBean appRecomendBean;

    private AppRecomendAdapter mAppRecomendAdapter;

    private ImageView nodataImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        action = "friendApp";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_recommend);
        setTitle("应用推荐");

        mListView = (ListView) findViewById(R.id.list_view);
        nodataImageView = (ImageView) findViewById(R.id.nodata);
        appRecomendBean = new AppRecomendBean();
        mAppRecomendAdapter = new AppRecomendAdapter(this, appRecomendBean);
        mListView.setAdapter(mAppRecomendAdapter);
        loadingDialog = new LoadingDialog(this);

        mNetDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                loadingDialog.dismiss();
                Toast.makeText(AppRecommendActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                loadingDialog.dismiss();
                Gson gson = new Gson();
                appRecomendBean = gson.fromJson(json, AppRecomendBean.class);
                if ("success".equals(appRecomendBean.getStatus())) {
                    if (appRecomendBean.getData() == null || appRecomendBean.getData().size() == 0) {
                        nodataImageView.setVisibility(View.VISIBLE);
                    } else {
                        mAppRecomendAdapter.setData(appRecomendBean);
                    }
                }else {
                    Toast.makeText(AppRecommendActivity.this, appRecomendBean.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        }, 1);
        mNetDataJson.setUrl(API.app_recommend);
        mNetDataJson.addParam("client", "android");
        mNetDataJson.request("get");
        loadingDialog.show();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mNetDataJson!=null){
            mNetDataJson.cancelTask();
            mNetDataJson = null;
        }
    }

}
